$(function () {
    function cha() {
        let sele=document.getElementsByName("userImg")
        let op="<option value='0'>---请选择---</option>"
        $.ajax({
            url: "/Role/All",
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                for (let j=0;j<sele.length;j++){
                    sele[j].innerHTML=op;
                    for (var i=0;i<text.length;i++){
                        var option="<option value="+text[i].roleId+">"+text[i].roleName+"</option>"
                        sele[j].innerHTML+=option
                    }
                }
            }
        })
    }
    cha()
    function author() {
        let menu=$("#menu")
        $.ajax({
            url: "/aut/all/",
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                for(let i=0;i<text.length;i++){
                    let li="<li><input type=\"checkbox\" value='"+text[i].auId+"'><span>"+text[i].auName+"</span></li>"
                    menu.append(li)
                }
            }
        })
    }

    author()

    $("select").change(function () {
        $("input").prop("checked",false)
        let roleId=$("#role").val()
        $.ajax({
            url: "/aut/author/"+roleId,
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                for(let i=0;i<text.length;i++){
                    $("input[value='"+text[i].auId+"']").prop("checked",true)
                }
            }
        })
    })
})

function RoleAuthor() {
    let roleId = $("#role").val()
    let arr = [];
    $("input:checkbox:checked").each(function (i) {
        arr.push($(this).val());
    });

    if(roleId!=0){
        if (confirm("确定要修改该角色的权限吗？")){
            $.ajax({
                url: "/aut/" + roleId,
                type: "delete",
                success: function (text) {
                    $.ajax({
                        url: "/aut/add/",
                        type: "post",
                        data: {
                            roleId: roleId,
                            arr: arr.toString()
                        },
                        success: function (text) {
                            parent.window.location.reload();
                        }
                    })
                }
            })
        }

    }else {
        alert("请选择你需要授予权限的角色")
    }

}